package leetcode;

import org.junit.Test;

import java.util.HashMap;

/**
 * @author cino
 * justCode
 */
public class leetcode290 {

    @Test
    public void test(){
        System.out.println(wordPattern("abba", "dog cat cat dog"));
    }

    public boolean wordPattern(String pattern, String s) {
        HashMap<Character, String> map = new HashMap<>();
        int len = pattern.length();
        String[] str = s.split(" ");
        if(len != str.length) return false;
        for(int i =0;i<len;i++){
            char key = pattern.charAt(i);
            if(!map.containsKey(key)){
                if(map.containsValue(str[i])){
                    return false;
                }
                map.put(key,str[i]);
            }else if(!map.get(key).equals(str[i])){
                return false;
            }
        }
        return true;
    }
}
